#include "sourceframemodel.h"

const int SourceFrameModel::MAX_COLUMN_NUM = 1;

SourceFrameModel::SourceFrameModel(PlistParser* plistParser,QObject *parent) :
    QAbstractListModel(parent),
    m_plistParser(plistParser)
{
}

SourceFrameModel::~SourceFrameModel()
{
}

void SourceFrameModel::dataChangeUpdate()
{
    reset();
}

bool SourceFrameModel::isValidIndex(QModelIndex index) const
{
    if(index.row() < 0 || index.row() >= m_plistParser->getPlistFrameInfoList().count())
    {
        return false;
    }

    return true;
}

QVariant SourceFrameModel::data(const QModelIndex &index, int role) const
{
    if(!isValidIndex(index)) return QVariant();

    if(role == Qt::DisplayRole)
    {
         return m_plistParser->getPlistFrameInfoList().at(index.row())->name;
    }
    else
    {
        return QVariant();
    }
}

Qt::ItemFlags SourceFrameModel::flags(const QModelIndex &index) const
{
    if(!isValidIndex(index))
    {
        return Qt::ItemIsEnabled;
    }
    return QAbstractItemModel::flags(index);
}

QVariant SourceFrameModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
        switch (section) {
            case 0:
                return tr("Name");
            default:
                return QVariant();
        }
    }
    return QVariant();
}

int SourceFrameModel::columnCount(const QModelIndex &parent) const
{
    return MAX_COLUMN_NUM;
}

int SourceFrameModel::rowCount(const QModelIndex &parent) const
{
    return m_plistParser->getPlistFrameInfoList().count();
}

PlistFrameInfo* SourceFrameModel::getInfoData(QModelIndex& index)
{
     if(!isValidIndex(index)) return NULL;

     return m_plistParser->getPlistFrameInfoList().at(index.row());
}
